package com.example.qihuibao.module.service

import android.app.IntentService
import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import com.example.qihuibao.utils.CommonUtil
import com.example.qihuibao.utils.LogUtil

/**
 * 定时任务
 */
class TimerService : Service() {
    private var callback:CallBack? = null
    private var threadRunning = true

    override fun onCreate() {
        super.onCreate()
//        //检查网络是否可用
//        Thread {
//            while (threadRunning) {
//                val status = CommonUtil.isConnectNetwork()
//                LogUtil.e("onHandleIntent:$status")
//                this.callback?.getNetworkStatus(status)
//                Thread.sleep(60000L)
//            }
//        }.start()
//
//        //同步数据
//        Thread {
//            while (true) {
//                this.callback?.synchronize()
//                Thread.sleep(60000L)
//            }
//        }.start()
        Thread {
            while (true) {
                this.callback?.scheduledTasks()
                Thread.sleep(1000)
            }
        }.start()
    }

    override fun onBind(intent: Intent?): IBinder? {
        return MyBinder()
    }

    override fun onDestroy() {
        super.onDestroy()
        threadRunning = false
    }

    fun setCallback(callBack: CallBack) {
        this.callback = callBack
    }

    inner class MyBinder : Binder() {
        fun getService(): TimerService {
            return this@TimerService
        }
    }

    interface CallBack {
        fun scheduledTasks()
    }
}